<?php
class Task
{
	protected $db = null;
	
	protected $taskId = null;
	
	protected $data = array(
		'add_datetime' 		=> null,
		'edit_datetime' 	=> null,
		'status_id'			=> 0,
		'priority_id'		=> 0,
		'subject' 			=> '',
		'description'		=> ''
	);
	
	protected $assignedUsers = array();
	
	protected $assignedUsersCache = array();
	
	public function __construct($db, $taskId = null)
	{
		$this->db = $db;
		
		if($taskId !== null)
		{
			$this->taskId = $taskId;
			$this->load();
		}
	}
	
	public function getAddDatetime() { return $this->data['add_datetime']; }
	public function getEditDatetime() { return $this->data['edit_datetime']; }
	public function setStatusId($statusId) { $this->data['status_id'] = $statusId; }
	public function getStatusId() { return $this->data['status_id']; }
	public function getStatus() { return (string)$this->data['status_id']; }
	public function setPriorityId($priorityId) { $this->data['priority_id'] = $priorityId; }
	public function getPriorityId() { return $this->data['priority_id']; }
	public function getPriority() { return (string)$this->data['priority_id']; }
	public function setSubject($subject) { $this->data['subject'] = $subject; }
	public function getSubject() { return $this->data['subject']; }
	public function setDescription($description) { $this->data['description'] = $description; }
	public function getDescription() { return $this->data['description']; }
	
	public function getAssignedUserIds() 
	{
		return $this->assignedUsers;
	}
	
	public function assignUser($userId) 
	{
		if(in_array($userId, $this->assignedUsers) === false)
		{
			$this->assignedUsers[] = $userId;
		}
	}
	
	public function unassignUser($userId)
	{
		unset($this->assignedUsers[array_search($userId, $this->assignedUsers)]);
	}
	
	public function clearAssignedUsers()
	{
		$this->assignedUsers = array();
	}
	
	public function update()
	{
		$data = array(
			'edit_datetime'		=> $this->data['edit_datetime'],
			'status_id'			=> $this->data['status_id'],
			'priority_id'		=> $this->data['priority_id'],
			'subject'			=> $this->data['subject'],
			'description'		=> $this->data['description']
		);
		
		$this->db->update('tasks', $data, "task_id = '" . (int)$this->taskId) . "'";
		
		$this->updateAssignedUsers();
		
		$this->load();
	}
	
	public function create()
	{
		$data = array(
			'add_datetime'		=> $this->data['add_datetime'],
			'status_id'			=> $this->data['status_id'],
			'priority_id'		=> $this->data['priority_id'],
			'subject'			=> $this->data['subject'],
			'description'		=> $this->data['description']
		);
		
		$this->db->insert('tasks', $data);
		$this->taskId = $this->db->lastInsertId();
		
		$this->updateAssignedUsers();
		
		$this->load();
	}
	
	protected function load()
	{
		$sql = "SELECT
					add_datetime,
					edit_datetime,
					status_id,
					priority_id,
					subject,
					description
				FROM tasks
				WHERE
					task_id = ?";
		$result = $this->db->fetchRow($sql, $this->taskId);
		
		$this->data['add_datetime'] 	= $result['add_datetime'];
		$this->data['edit_datetime'] 	= $result['edit_datetime'];
		$this->data['status_id'] 		= $result['status_id'];
		$this->data['priority_id'] 		= $result['priority_id'];
		$this->data['subject'] 			= $result['subject'];
		$this->data['description'] 		= $result['description'];
		
		$this->loadAssignedUsers();
	}
	
	protected function updateAssignedUsers()
	{
		// delete removed users
		foreach($this->assignedUsersCache as $userId)
		{
			if(array_key_exists($userId, $this->assignedUsers) === false)
			{
				$this->db->delete('tasks_users', "task_id = '" . (int)$this->taskId . "' AND user_id = '" . (int)$userId . "'");
			}
		}
		
		// create added users
		foreach($this->assignedUsers as $userId)
		{
			if(array_key_exists($userId, $this->assignedUsersCache) === false)
			{
				$this->db->insert('tasks_users', array('task_id' => $this->taskId, 'user_id' => $userId));
			}
		}
	}
	
	protected function loadAssignedUsers()
	{
		$sql = "SELECT
					user_id
				FROM tasks_users
				WHERE
					task_id = ?";
		$result = $this->db->fetchAll($sql, $this->taskId);
		
		$this->clearAssignedUsers();
		foreach($result as $row)
		{
			$this->assignedUsers[] = $row['user_id'];
		}
		
		$this->assignedUsersCache = $this->assignedUsers;
	}
}